package com.naiterui.ehp.bs.cms.modules.wms.controller;

import java.io.IOException;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.cms.modules.wms.feign.ISkuFeignClient;
import com.naiterui.ehp.bs.cms.modules.wms.service.ISkuService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.SkuManageVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.SkuVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 *      sku
 * </pre>
 *
 * @author 王泽浩
 * @date 2019-05-20 19:29
 * @since 1.0.0
 */
@Slf4j
@Validated
@RestController
@Api(tags = {SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@RequestMapping("wms/medicine/sku")
public class SkuController {

  @Autowired
  private ISkuService skuService;
  @Autowired
  private ISkuFeignClient skuFeignClient;

  /**
   * 列表
   *
   * @return
   */
  @SysLog("SKU列表")
  @ApiOperation(value = "SKU列表")
  @GetMapping("/list/{productId}")
  @PreAuthorize("@p.check('wms:medicine:sku:list')")
  public SkuManageVO list(@PathVariable Long productId) {
    return this.skuService.getSkuManage(productId);
  }

  @SysLog("保存修改SKU:药品")
  @PostMapping("/update")
  @ApiOperation(value = "保存修改SKU:药品")
  @PreAuthorize("@p.check('wms:medicine:sku:update')")
  public SkuManageVO update(@Valid @RequestBody SkuVO skuVO) throws BusinessException {
    this.skuService.updateSku(skuVO);
    return this.skuService.getSkuManage(skuVO.getProductId());
  }

  @SysLog("设置默认sku")
  @PostMapping("/default/{productId}/{skuId}")
  @ApiOperation(value = "设置默认sku")
  @PreAuthorize("@p.check('wms:medicine:sku:update')")
  public SkuManageVO updateDefatule(@PathVariable("productId") Long productId, @PathVariable("skuId") Long skuId) throws BusinessException {
    this.skuFeignClient.updateDefatule(productId, skuId);
    return this.skuService.getSkuManage(productId);
  }

  @ApiOperation(value = "上传sku图片")
  @ApiImplicitParam(name = "file", value = "sku图片", dataType = "__file", paramType = "form", required = true)
  @PostMapping("/upload")
  @PreAuthorize("@p.check('wms:medicine:sku:upload')")
  public ResponseVO<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file) throws IOException {
    String url = this.skuService.upload(file);
    return PublicService.returnResponseVO(url);
  }

}
